<?php  
	include("../includes/connect_db.php");
	include("../includes/core.php");

	$id 				= $_POST['id'];
	$nama 			= $_POST['nama'];
	$kode 			= $_POST['kode'];
	$keterangan = $_POST['keterangan'];
	$act 				= $_POST['act'];
	$act_form 	= $_POST['act_form'];

	if (!isset($_SESSION['idUser'])) {
		$iduser = 0;
	} else {
		$iduser = $_SESSION['idUser'];
	}

	switch ($act) {
		case 'valid_act':
			if ($act_form == "new") {
				$data_tipe = validateAddName($nama,"tipe_user");
			}else{
				$data_tipe = validateEditName($id,$nama,"tipe_user");
			}

			if ($data_tipe > 0) {
				echo "1";
			}else{
				echo "0";
			}
			break;

		case 'valid_code':
			if ($act_form == "new") {
				$data_tipe = validateAddCode($kode,"tipe_user");
			}else{
				$data_tipe = validateEditCode($id,$kode,"tipe_user");
			}

			if ($data_tipe > 0) {
				echo "1";
			}else{
				echo "0";
			}
			break;
			
		case '1':
			$sql_insert_tipe_user = "INSERT INTO tipe_user (nama, kode, keterangan, iduser) VALUES ('$nama', '$kode', '$keterangan', '$iduser')";
			if ($query_insert_tipe_user = mysql_query($sql_insert_tipe_user)) {
				echo "sukses";
			} else {
				echo mysql_error();
			}
			
			break;
		
		case '2':
			$sql_update_tipe_user = "UPDATE tipe_user SET nama = '$nama', kode = '$kode', keterangan = '$keterangan' WHERE id = '$id'";
			if ($query_update_tipe_user = mysql_query($sql_update_tipe_user)) {
				echo "sukses";
			} else {
				echo mysql_error();
			}

			break;

		default:
			# code...
			break;
	}
?>